package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.ChronicDiseaseCategory;
import com.lxw.nonghe.mapper.ChronicDiseaseCategoryMapper;
import com.lxw.nonghe.service.ChronicDiseaseCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class ChronicDiseaseCategoryServiceImpl extends ServiceImpl<ChronicDiseaseCategoryMapper, ChronicDiseaseCategory> implements ChronicDiseaseCategoryService {
    @Override
    public Page<ChronicDiseaseCategory> getDiseasePage(int current, int size, String diseaseName) {
        Page<ChronicDiseaseCategory> page = new Page<>(current, size);
        return (Page<ChronicDiseaseCategory>) this.baseMapper.selectChronicDiseaseCategoryPage(page, diseaseName);
    }

    @Override
    public ChronicDiseaseCategory getDiseaseByName(String diseaseName) {
        return this.lambdaQuery().eq(ChronicDiseaseCategory::getDiseaseName, diseaseName).one();
    }

    @Override
    public Page<ChronicDiseaseCategory> queryDisease(ChronicDiseaseCategory queryDisease, int current, int size) {
        LambdaQueryWrapper<ChronicDiseaseCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryDisease.getDiseaseId() != null,
                        ChronicDiseaseCategory::getDiseaseId, queryDisease.getDiseaseId())
                .eq(queryDisease.getStatus() != null,
                        ChronicDiseaseCategory::getStatus, queryDisease.getStatus())
                .eq(StringUtils.isNotBlank(queryDisease.getDiseaseCode()),
                        ChronicDiseaseCategory::getDiseaseCode, queryDisease.getDiseaseCode())
                .eq(StringUtils.isNotBlank(queryDisease.getPinyinCode()),
                        ChronicDiseaseCategory::getPinyinCode, queryDisease.getPinyinCode())
                .like(StringUtils.isNotBlank(queryDisease.getDiseaseName()),
                        ChronicDiseaseCategory::getDiseaseName, queryDisease.getDiseaseName())
                .like(StringUtils.isNotBlank(queryDisease.getDescription()),
                        ChronicDiseaseCategory::getDescription, queryDisease.getDescription())
                .orderByDesc(ChronicDiseaseCategory::getCreateTime);
        Page<ChronicDiseaseCategory> page = new Page<>(current, size);
        return this.page(page, wrapper);
    }
}
